package com.yl.order.helper;

import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.yl.common.utils.LogUtil;
import com.yl.order.interfaces.OnScanGunCallback;

/**
 * Created by chenjinxu on 2019/7/2.
 * 说明: 扫码枪拦截解析工具
 */
public class ScanCodeHelper {
    private volatile static ScanCodeHelper helper;

    private final int SCAN_DELAY = 120;

    // 扫码通知队列
    private Handler handler = new Handler();

    // 扫码结果
    private StringBuilder builder = new StringBuilder();

    // 扫码结果回调
    private OnScanGunCallback callback;

    // 是否按下shift键
    private boolean shiftDown = false;

    // 扫码结束处理函数
    private Runnable scanFinishRunnable = new Runnable() {
        @Override
        public void run() {
            final String code = builder.toString().trim();
            if (TextUtils.isEmpty(code)) {
                builder = new StringBuilder();
                return;
            }
            LogUtil.getInstance().log("SCAN_USB", "scanFinishRunnable: " + code);
            if (callback != null) {
                callback.scanResult(code, null);
            }
            builder = new StringBuilder();
        }
    };

    private ScanCodeHelper() {

    }

    public static ScanCodeHelper getInstance() {
        if (helper == null) {
            synchronized (ScanCodeHelper.class) {
                if (helper == null) {
                    helper = new ScanCodeHelper();
                }
            }
        }
        return helper;
    }

    /**
     * 设置扫码结果回调函数
     */
    public void setScanGunCallback(OnScanGunCallback callback) {
        this.callback = callback;
    }

    /**
     * 解析扫码枪触发的键盘事件
     */
    public void parserKeyEvent(KeyEvent event) {
        switch (event.getAction()) {
            case KeyEvent.ACTION_DOWN:
                handler.removeCallbacks(scanFinishRunnable);
                int keyCode = event.getKeyCode();
                if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT) {
                    shiftDown = true;
                } else if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    handler.post(scanFinishRunnable);
                } else {
                    char c = getInputCode(event);
                    builder.append(c);
                    handler.postDelayed(scanFinishRunnable, SCAN_DELAY);
                }
                break;
            case KeyEvent.ACTION_UP:
                keyCode = event.getKeyCode();
                if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT) {
                    shiftDown = false;
                }
                break;
        }
    }

    //获取扫描内容
    private char getInputCode(KeyEvent event) {

        int keyCode = event.getKeyCode();

        char aChar;

        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
            //字母
            aChar = (char) ((shiftDown ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
        } else {
            //其他符号
            switch (keyCode) {
                case KeyEvent.KEYCODE_0:
                    aChar = shiftDown ? ')' : '0';
                    break;
                case KeyEvent.KEYCODE_1:
                    aChar = shiftDown ? '!' : '1';
                    break;
                case KeyEvent.KEYCODE_2:
                    aChar = shiftDown ? '@' : '2';
                    break;
                case KeyEvent.KEYCODE_3:
                    aChar = shiftDown ? '#' : '3';
                    break;
                case KeyEvent.KEYCODE_4:
                    aChar = shiftDown ? '$' : '4';
                    break;
                case KeyEvent.KEYCODE_5:
                    aChar = shiftDown ? '%' : '5';
                    break;
                case KeyEvent.KEYCODE_6:
                    aChar = shiftDown ? '^' : '6';
                    break;
                case KeyEvent.KEYCODE_7:
                    aChar = shiftDown ? '&' : '7';
                    break;
                case KeyEvent.KEYCODE_8:
                    aChar = shiftDown ? '*' : '8';
                    break;
                case KeyEvent.KEYCODE_9:
                    aChar = shiftDown ? '(' : '9';
                    break;
                case KeyEvent.KEYCODE_PERIOD:
                    aChar = shiftDown ? '>' : '.';
                    break;
                case KeyEvent.KEYCODE_EQUALS:
                    aChar = shiftDown ? '+' : '=';
                    break;
                case KeyEvent.KEYCODE_GRAVE:
                    aChar = shiftDown ? '~' : '`';
                    break;
                case KeyEvent.KEYCODE_MINUS:
                    aChar = shiftDown ? '_' : '-';
                    break;
                case KeyEvent.KEYCODE_SLASH:
                    aChar = shiftDown ? '?' : '/';
                    break;
                case KeyEvent.KEYCODE_BACKSLASH:
                    aChar = shiftDown ? '|' : '\\';
                    break;
                case KeyEvent.KEYCODE_LEFT_BRACKET:
                    aChar = shiftDown ? '{' : '[';
                    break;
                case KeyEvent.KEYCODE_RIGHT_BRACKET:
                    aChar = shiftDown ? '}' : ']';
                    break;
                case KeyEvent.KEYCODE_SEMICOLON:
                    aChar = shiftDown ? ':' : ';';
                    break;
                case KeyEvent.KEYCODE_COMMA:
                    aChar = shiftDown ? '<' : ',';
                    break;
                case KeyEvent.KEYCODE_APOSTROPHE:
                    aChar = shiftDown ? '"' : '\'';
                    break;
                case KeyEvent.KEYCODE_SPACE:
                    aChar = ' ';
                    break;
                default:
                    aChar = 0;
                    break;
            }
        }
        return aChar;

    }
}
